package com.cloudpan;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;


public class AutoCodeGenerator {

    public static void main(String[] args) {
        // 代码生成器对象
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        // 项目路径
        //String projectPath = System.getProperty("user.dir");
        String projectPath = AutoCodeGenerator.class.getResource("").getPath();
        projectPath = (projectPath.substring(1,projectPath.indexOf("target"))).replace("/","\\");
        // 输出路径
        gc.setOutputDir(projectPath + "/src/test/java/entity");
        // 作者
        gc.setAuthor("Code Generator");
        // 是否打开资源管理器
        gc.setOpen(true);
        // 是否覆盖原来代码
        gc.setFileOverride(false);
        // 去掉service的i前缀
        gc.setServiceName("%sService");
        // 主键策略
        gc.setIdType(IdType.ASSIGN_ID);
        // swagger
        gc.setSwagger2(true);
        //
        mpg.setGlobalConfig(gc);

        // 设置数据源
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/cloudpan?useUnicode=true&characterEncoding=utf8" +
                "&useSSL=true");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("123456");
        dataSourceConfig.setDbType(DbType.MYSQL);
        mpg.setDataSource(dataSourceConfig);
        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName("");
        packageConfig.setParent("com.clodpan");
        packageConfig.setEntity("domain.pojo");
        packageConfig.setMapper("mapper");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("serviceImpl");
        packageConfig.setController("controller");
        mpg.setPackageInfo(packageConfig);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // 设置表名
        strategy.setInclude("sys_user_message");
        //strategy.setInclude("linkapps_info_attention","linkapps_info_column","linkapps_info_detail","linkapps_info_main","linkapps_info_publish_task","linkapps_info_recipient","linkapps_info_template","linkapps_info_top_task");

        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        // 逻辑删除
        strategy.setLogicDeleteFieldName("del_flag");
        // 自动填充
        TableFill create_time = new TableFill("create_time", FieldFill.INSERT);
        TableFill create_user_id = new TableFill("create_by", FieldFill.INSERT);
        TableFill update_time = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        TableFill update_user_id = new TableFill("update_by", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> objects = new ArrayList<>();
        objects.add(create_time);
        objects.add(create_user_id);
        objects.add(update_time);
        objects.add(update_user_id);
        strategy.setTableFillList(objects);
        // 乐观锁
        strategy.setVersionFieldName("version");
        // 开始controller的驼峰
        strategy.setRestControllerStyle(true);
        //
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);

        mpg.execute();
    }
}
